"""common.py模块存储的是自定义工具"""

import functools
from flask import session, current_app, g


def click_news_filter(index):
    """自定义过滤器,过滤点击排行html的class"""
    if index == 0:
        return "first"
    elif index == 1:
        return "second"
    elif index == 2:
        return "third"
    else:
        return ""


def get_user_info(f):
    """自定义装饰器,获取用户登录信息"""

    @functools.wraps(f)  # 每次调用完f之后,清空f的内容
    def wrapper(*args, **kwargs):
        """添加获取用户登录后的个人信息代码"""
        user_id = session.get('user_id')
        user = None
        if user_id:
            try:
                from info.models import User
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return f(*args, **kwargs)

    return wrapper
